package com.hd.trans.ui.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

abstract class DataBindingBaseFragment<T: ViewDataBinding>: BaseFragment() {
    private var contentView: View? = null
    protected lateinit var mDataBinding: T

    abstract fun getLayoutId(): Int
    abstract fun initViewsAndEvents(savedInstanceState: Bundle?)
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if(contentView == null){
            contentView = inflater.inflate(getLayoutId(), container, false)
            mDataBinding = DataBindingUtil.bind(contentView!!)!!
            mDataBinding.lifecycleOwner = this
            initViewsAndEvents(savedInstanceState)
        }else{
            contentView!!.parent?.let {
                (it as ViewGroup).removeView(contentView)
            }
        }
        return contentView
    }
}